{extend name="base"}
{block name="title"}
点赚金-后台管理-分类管理
{/block}
{block name="pageName"}
分类列表
{/block}
{block name="pageBody"}
<!-- 一个表格 -->
<div class="btn-toolbar">
    <button class="btn btn-primary" data-title="Add catrgory" data-toggle="modal" data-target="#add_category"><i class="icon-plus"></i>添加分类</button>
    <button class="btn btn-success" onclick="javascript:location.reload()">刷新</button>
  <div class="btn-group">
  </div>
</div>
<div class="well">
    <table class="table">
      <thead>
        <tr>
			<th>#</th>
			<th>分类名称</th>
			<th>分类下商品数量</th>
			<th>状态</th>
			<th>操作</th>
          	<th style="width: 26px;"></th>
        </tr>
      </thead>
      <tbody id="categorys">
        {volist name='categorys' id='category'}
		<tr id="{$category.category_id}">
			<th scope="row">{$i}</th>
			<td>
				<input type="text" value="{$category.category_name}" onchange="edit('{$category.category_id}')">
			</td>
			<td>{$category.num}</td>
			<td>
				{if condition="$category.is_delete eq 0"}
					<span class="badge badge-success">正常</span>
					{else/}
					<span class="badge badge-danger">禁用</span>
				{/if}		
			</td>
			<td>
				<div class="dropdown">
					{if condition="$category.is_delete eq 0"}
					<button class="btn btn-primary dropdown-toggle" onclick="disable('{$category.category_id}')">禁用</button>
					{else/}
					<button class="btn btn-primary dropdown-toggle" onclick="enable('{$category.category_id}')">启用</button>
					{/if}
				</div>
			</td>
		</tr>
		{/volist}
      </tbody>
    </table>
</div>
<div class="pagination">
</div>

<!-- 进行状态修改的jQuery函数 -->
	<script>
		function disable(category_id){
			$.getJSON("{:url('doCategoryDisable')}",{category_id:category_id},function(data){
				if(data.code == 0){
				  $("#"+data.data.category_id+" td").eq(2).html('<span class="badge badge-danger">禁用</span>');
				  $("#"+data.data.category_id+" td").eq(3).html("<button class='btn btn-primary dropdown-toggle' onclick='enable(\""+data.data.category_id+"\")'>启用</button>");
				}else{
				  alert(data.msg);
				}
				
			})	
		}
		function enable(category_id){
			$.getJSON("{:url('doCategoryEnable')}",{category_id:category_id},function(data){
				$("#"+data.data.category_id+" td").eq(2).html('<span class="badge badge-success">正常</span>');
				$("#"+data.data.category_id+" td").eq(3).html("<button class='btn btn-primary dropdown-toggle' onclick='disable(\""+data.data.category_id+"\")'>禁用</button>");
			})
		}
		function edit(category_id){
			editName = $("#"+category_id+" input").eq(0).val();
			console.log(editName);
			$.getJSON("{:url('editCategory')}",{category_id:category_id,editName:editName},function(data){
				alert(data.data);
			})
		}
	</script>
{/block}
<!-- 一个表格结束 -->

{block name="model"}
	<script>
		function addCategory(){
			var form = new FormData(document.getElementById("addcategory"));
			$.ajax({
                url:"{:url('addCategory')}",
                type:"post",
                data:form,
                processData: false,                // jQuery不要去处理发送的数据
    			contentType: false,
    			dataType: 'json',
                success:function(data){
                    if (0 == data.code) {
                    	var category = data.data;
                    	var html = "<tr>";
                    		html += "<td>-</td>";
	                    	html += "<td>"+category["category_name"]+"</td>";
	                    	html += "<td>0</td>";
	                    	html += "<td><span class='badge badge-success'>正常</span></td>";
	                    	html += "<td>-</td>";
                    	html+="</tr>";
                    	$("#categorys").prepend(html);
                    	$('#add_category').modal('hide');
                    }else{
                    	alert(data.msg);
                    }
                }
            }); 
		}
	</script>
	<div id="add_category" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">添加分类</h3>
		</div>
		<form id="addcategory" class="form-horizontal" onsubmit="return false;" method="post">
			<br>
			<div class="control-group">
	            <label class="control-label">分类名</label>
	            <div class="controls">
	              <input type="text" name="category_name" placeholder="请输入分类名称" required><br>
	        	</div>
	        </div>
		</form>
		<div class="modal-footer">
			<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
			<input class="btn" type="button" value="提交" onclick="addCategory();">
		</div>
	</div>
{/block}